package modeller.databasedesignmodel.transaction;

import modeller.databasedesignmodel.servicedemand.transaction.proceduralstatement.IProceduralStatementServiceDemandCalculator;
import modeller.databasedesignmodel.servicedemand.IServiceDemand;

import java.io.Serializable;

/**
 * Created by:  Jason Ye
 * Date:        25/02/2012
 * Time:        12:43
 */
public class Loop implements IProceduralStatement, Serializable {

    private int numIterations;
    private IProceduralStatement loopContents;

    public Loop(int iter, IProceduralStatement contents) {

        loopContents = contents;
        numIterations = iter;
    }

    public Loop(int iter) {
        this(iter,new SQLQuerySequence());
    }

    public int getNumIterations() {
        return numIterations;
    }

    public void setNumIterations(int numIterations) {
        this.numIterations = numIterations;
    }

    public IProceduralStatement getLoopContents() {
        return loopContents;
    }

    public void setLoopContents(IProceduralStatement loopContents) {
        this.loopContents = loopContents;
    }


    @Override
    public String toString() {

        return myToString();
    }

    @Override
    public String myToString() {
        StringBuffer buf = new StringBuffer();
        buf.append("\nLoop with " + numIterations + " iterations:");
        buf.append("\nLoop body: \n" + loopContents.toString());
        buf.append("\nEnd of Loop");
        return buf.toString();
    }

    @Override
    public IServiceDemand acceptCalculation(IProceduralStatementServiceDemandCalculator visitor) {
        return visitor.calculate(this);
    }
}
